﻿package  com.winglish.as3.record{
	import flash.events.Event;
	import flash.media.SoundChannel;
	import org.bytearray.micrecorder.MicRecorder;
	import org.bytearray.micrecorder.encoder.WaveEncoder;	
	import org.bytearray.micrecorder.events.RecordingEvent;
	import org.as3wavsound.WavSound;	
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.EventDispatcher;
	
	public class Recorder extends EventDispatcher{
		private var _micRecorder: MicRecorder;
		private var _wavSound: WavSound;
		public var _channel:SoundChannel;
		private var _isRecord: Boolean;
		private var timer:Timer;
		
		public function Recorder() {}
		private function setMicRecorder(): void{
			_micRecorder  = new MicRecorder( new WaveEncoder() );
			_isRecord = false;
		}
		
		/**
		 * 이벤트 등록.
		 */
		private function addEvent(): void{			
			_micRecorder.addEventListener( RecordingEvent.RECORDING, onRecording);
			_micRecorder.addEventListener( Event.COMPLETE, onRecordComplete);
		}
		
		/**
		 * 이벤트 삭제.
		 */
		private function removeEvent(): void{
			_micRecorder.removeEventListener( RecordingEvent.RECORDING, onRecording);
			_micRecorder.removeEventListener( Event.COMPLETE, onRecordComplete);
		}
		
		/**
		 * 녹음 중 이벤트.
		 * @param	$e
		 */
		private function onRecording( e:RecordingEvent ): void{
			//trace( "recording..." );
		}
		
		/**
		 * 녹음 완료 이벤트.
		 * @param	e
		 */
		private function onRecordComplete( e: Event ): void{
			_wavSound = new WavSound( _micRecorder.output );
		}
		
		/**
		 * 녹음 시작.
		 */
		public function start(): void{
			setMicRecorder();
			addEvent();
			_micRecorder.record();
			_isRecord = true;
			trace ( "recordSTART" );
		}
		
		/**
		 * 녹음 정지.
		 */
		public function stop(): void{
		
			if( _isRecord ){
				_micRecorder.stop();
				removeEvent();
				_isRecord = false;
				trace ( "recordStop" );
			}
			else{
				//trace( "녹음중이지 않습니다. 정시시킬 녹음이 없습니다." );
			}
		}
		
		/**
		 * 녹음 듣기.
		 */
		public function listen(sec:int = 0): void {
			trace ( "recordListen" );
			if( _wavSound ){
				_wavSound.stop();
				_channel = _wavSound.play(sec);
				timer = new Timer(1,int(_wavSound.length/10));
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, fnListenEnd);
				timer.start();
				
			}
		}
		
		/**
		 * 녹음 듣기 끝 이벤트.
		 * @param	e
		 */
		private function fnListenEnd(e:TimerEvent):void{
			//trace("end");
			dispatchEvent(new Event("recListenEnd"));
		}
		
		/**
		 * 녹음 듣기 정지.
		 */
		public function stopListen(): void{
			if( _wavSound ) _wavSound.stop(); 
			if(timer) timer.reset();
		}
	
		/**
		 * 녹음 사운드 chaneel.
		 * @return
		 */
		public function curchannel():SoundChannel{
			return _channel;
		}
		
		/**
		 * 마이크가 감지하는 사운드 크기.
		 * @return
		 */
		public function getLev():Number{
			return _micRecorder.microphone.activityLevel;
		}
		
		//public function setVolume( $num: Number ): void{
//			if( _wavSound ) _wavSound.setVolume( $num );
//		}
	}	
}
